package com.cjhhv.visual.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.cjhhv.base.model.ResponseDataModel;
import com.cjhhv.base.util.ResponseDataUtil;
import com.cjhhv.visual.model.DashboardModel;
import com.cjhhv.visual.service.DashboardService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dashboard")
public class DashboardController {


    private DashboardService dashboardService;


    @GetMapping("/initDashboardListData")
    @ResponseBody
    public ResponseDataModel initDashboardListData(){
        Map<String,Object> data  = this.dashboardService.initDashboardListData();
        return ResponseDataUtil.createResponseData(true,"查询成功!",data);
    }


    @PostMapping("/queryDashboardPageList")
    @ResponseBody
    public ResponseDataModel queryDashboardPageList(@RequestBody DashboardModel parameter){
        PageInfo<DashboardModel> pageInfo = this.dashboardService.queryDashboardPageList(parameter);
        return ResponseDataUtil.createResponseData(true,"查询成功!",pageInfo);
    }



    @GetMapping("/initDashboardAddData")
    @ResponseBody
    public ResponseDataModel initDashboardAddData(@RequestParam("id") String id ){
        Map<String,Object> data  = this.dashboardService.initDashboardAddData(id);
        return ResponseDataUtil.createResponseData(true,"查询成功!",data);
    }

    @GetMapping("/queryDashboardById")
    @ResponseBody
    public ResponseDataModel queryDashboardById(@RequestParam("id") String id ){
       DashboardModel dashboardModel = this.dashboardService.queryDashboardById(id);
       if(ObjectUtil.isNotEmpty(dashboardModel)){
           return ResponseDataUtil.createResponseData(true,"查询成功!",dashboardModel);
       }else{
           return ResponseDataUtil.createResponseData(false,"查询失败!",null);
       }
    }


    @PostMapping("/saveDashboard")
    @ResponseBody
    public ResponseDataModel saveDashboard(@RequestBody DashboardModel parameter){
        int count = this.dashboardService.queryDashboardIsExist(parameter);
        if(count>0){
            return ResponseDataUtil.createResponseData(false,parameter.getDashboardTitle()+"已存在！",null);
        }
        DashboardModel dashboardModel = this.dashboardService.saveDashboard(parameter);
        if(ObjectUtil.isEmpty(dashboardModel)){
            return ResponseDataUtil.createResponseData(false,"保存失败!",null);
        }
        return ResponseDataUtil.createResponseData(true,"保存成功!",dashboardModel);
    }

    @PostMapping("/deleteDashboardByIds")
    @ResponseBody
    public ResponseDataModel deleteDashboardByIds(@RequestBody DashboardModel parameter) {
        int count =  this.dashboardService.deleteDashboardByIds(parameter);
        if(count>0){
            return ResponseDataUtil.createResponseData(true,"删除成功!",null);
        }else {
            return ResponseDataUtil.createResponseData(false,"删除失败!",null);
        }
    }


    public DashboardService getDashboardService() {
        return dashboardService;
    }

    @Autowired
    public void setDashboardService(DashboardService dashboardService) {
        this.dashboardService = dashboardService;
    }
}
